﻿using CloudFranceTriple.Models;
using System;
using System.Collections.Generic;
using System.Linq;

namespace CloudFranceTriple.WebRole
{
    public class VeteranConfig
    {
        private static VeteranWallPeriod PeriodAddMonth(VeteranWallPeriod period)
        {
            period.VeteranWallMonthPeriod++;
            if (period.VeteranWallMonthPeriod == 13)
            {
                period.VeteranWallMonthPeriod = 1;
                period.VeteranWallYearPeriod++;
            }
            return period;
        }

        private static void CreatePeriod(VeteranWallPeriod periodStart)
        {
            var now = System.DateTime.Now;
            var currentPeriod = new VeteranWallPeriod { VeteranWallYearPeriod = periodStart.VeteranWallYearPeriod, VeteranWallMonthPeriod = periodStart.VeteranWallMonthPeriod };

            using (var db2 = new cloudfrAboq2RsDMEntities())
            {
                do
                {
                    db2.VeteranWallPeriod.Add(currentPeriod);
                    currentPeriod = new VeteranWallPeriod { VeteranWallYearPeriod = currentPeriod.VeteranWallYearPeriod, VeteranWallMonthPeriod = currentPeriod.VeteranWallMonthPeriod };
                    currentPeriod = PeriodAddMonth(currentPeriod);
                } while ((currentPeriod.VeteranWallYearPeriod < now.Year) | (currentPeriod.VeteranWallMonthPeriod <= now.Month));

                db2.SaveChanges();
            }
        }

        public static void RegisterVeteranStatus()
        {
            using (var db = new cloudfrAboq2RsDMEntities())
            {
                if (db.VeteranStatus.Count() == 0)
                {
                    var AppVeteranStatus = new List<string> { "Fans", "Membre Baff", "Guildmaster Baff", "Membre FrTrpl",
                        "GuildMaster FrTrpl", "Recrut", "Veteran", "Déserteur", "Porté Disparu", "Banni","Réserviste" };
                    foreach (string thisStatus in AppVeteranStatus)
                    {
                        if (db.VeteranStatus.Where(r => r.Label == thisStatus).Count() == 0)
                        {
                            db.VeteranStatus.Add(new VeteranStatus { Label = thisStatus });
                        }
                    }
                    db.SaveChanges();
                }

                if (db.VeteranWallPeriod.Count() == 0)
                {
                    var periodMonthBegin = Int32.Parse(db.ApplicationSettingSet.Where(app => app.Name == "VeteranWallMonthBegin").First().Value);
                    var periodYearBegin = Int32.Parse(db.ApplicationSettingSet.Where(app => app.Name == "VeteranWallYearBegin").First().Value);
                    CreatePeriod(new VeteranWallPeriod { VeteranWallYearPeriod = periodYearBegin, VeteranWallMonthPeriod = periodMonthBegin });
                }
                else
                {
                    var lastPeriod = db.VeteranWallPeriod.Find(db.VeteranWallPeriod.Max(period => period.Id));
                    var now = System.DateTime.Now;
                    if (lastPeriod.VeteranWallMonthPeriod < now.Month || lastPeriod.VeteranWallYearPeriod < now.Year)
                    {
                        lastPeriod = PeriodAddMonth(lastPeriod);
                        CreatePeriod(lastPeriod);
                    }
                }
            }
        }
    }
}
